/**  
 * @project_name y_back
 * @Title: PwdControl.java
 * @Package com.yjd.back.user.control
 * @Description: TODO
 * @author lindy lindytem@126.com  
 * @date 2014-12-1 上午11:06:13
 * @version V1.0  
 * <b>Copyright (c)</b> 2014医信金融信息服务（上海）有限公司-版权所有<br/>
 */
package com.yjd.back.system.control;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.system.dto.AdminDataWrap;
import com.yjd.comm.system.model.AdminModel;
import com.yjd.comm.system.service.IAdminService;
import com.yjd.comm.system.util.AdminDataUtil;
import com.yjd.comm.util.SecurityCode;
import com.yjd.comm.util.ServletUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @ClassName: PwdControl
 * @Description: TODO
 * @author lindy lindytem@126.com
 * @date 2014-12-1 上午11:06:13
 */
@Controller
@Scope("prototype")
@RequestMapping("/pwdChange/")
public class PwdControl extends BaseControl {

	@Resource
	private IAdminService adminService;

	@RequestMapping("getMine")
	public String getMine() throws Exception {
		AdminDataWrap adminDataWrap = AdminDataUtil.getAdminDataWrap(request,
				response);

		AdminModel adminModel = adminDataWrap.getAdminModel();
		request.setAttribute("username", adminModel.getUsername());

		return "user.pwdChange";
	}

	@RequestMapping("set")
	public void set() throws Exception {
		AdminDataWrap adminDataWrap = AdminDataUtil.getAdminDataWrap(request,
				response);

		AdminModel adminModel = adminDataWrap.getAdminModel();
		Map<String, Object> paramMap = ServletUtil.getPageParameterMap(request);

		if (!paramMap.containsKey("oldpassword")
				|| !paramMap.containsKey("password")
				|| !paramMap.containsKey("password2")) {
			this.errorMsg(null, "请输入密码！");
			return;
		}

		if (paramMap.get("oldpassword").equals("")
				|| paramMap.get("password").equals("")
				|| paramMap.get("password2").equals("")) {
			this.errorMsg(null, "请输入密码！");
			return;
		}

		if (!paramMap.get("password").equals(paramMap.get("password2"))) {
			this.errorMsg(null, "两次输入密码不相同！");
			return;
		}
		if(!SecurityCode.passwordMD5_2(paramMap.get("oldpassword").toString(),adminModel.getEncrypt()).equals(adminModel.getPassword())){
			this.errorMsg(null, "原始密码不正确！");
			return;
		}
		
		if (paramMap.get("password").equals(paramMap.get("oldpassword"))) {
			this.errorMsg(null, "密码和原始密码一样！");
			return;
		}
		
		adminModel.setPassword(paramMap.get("password").toString());
		this.adminService.update(adminModel, getOperParamDto());

		this.successMsg("/admin/exit"+ ServletUtil.getUrlStuff(), "操作成功,重新登入！");
	}

}
